﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using Dqs_BLL;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Model;
using Sunny.UI;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace Dqs_UI
{
	public partial class frmAdmin_Student : UIForm
	{
		public frmAdmin_Student()
		{
			InitializeComponent();
		}
		Student_BLL student_BLL = new Student_BLL();
		Course_BLL course_BLL = new Course_BLL();


		private void frmAdmin_Student_Load(object sender, EventArgs e)
		{

			//加载课程
			List<CourseInfo> list = course_BLL.GetCourses();
			dgvCourseInfo.DataSource = list;

			CourseInfo[] courseInfos = list.ToArray();
			List<CourseInfo> list2 = new List<CourseInfo>();
			foreach (var item in courseInfos)
			{
				list2.Add(item);
			}
			list2.Insert(0, new CourseInfo() { CourseName = "--请选择--", Id = -1 });

			//复制数据源，避免下拉框和DGV出现联动问题


			//把list再次赋值给cmbCategory
			cmbCourseList.DisplayMember = "CourseName";
			cmbCourseList.ValueMember = "Id";
			cmbCourseList.DataSource = list2;

			//默认选择第一行
			this.dgvCourseInfo.Rows[0].Selected = true;

			//加载学生信息
			LoadStudentInfo();
		}

		private void LoadStudentInfo()
		{
			List<StudentInfo> list = student_BLL.GetStudents();
			dgvStudentInfo.DataSource = list;
		}


		private void btnInsertCourseInfo_Click(object sender, EventArgs e)
		{
			frmAddCourse frmAddCourse = new frmAddCourse();
			frmAddCourse.FormClosed += FrmAddCourse_FormClosed;
			frmAddCourse.ShowDialog();

		}

		private void FrmAddCourse_FormClosed(object? sender, FormClosedEventArgs e)
		{
			frmAdmin_Student_Load(this, e);
		}

		private void btnInsertStuInfo_Click(object sender, EventArgs e)
		{
			frmAddStudent frmAddStudent = new frmAddStudent();
			frmAddStudent.FormClosed += FrmAddCourse_FormClosed;
			frmAddStudent.ShowDialog();


		}

		private void cmbCourseList_SelectedIndexChanged(object sender, EventArgs e)
		{
			//获取选中类别的ID
			int courseid = Convert.ToInt32(cmbCourseList.SelectedValue);
			if (courseid == -1)
			{
				LoadStudentInfo();
			}
			else
			{
				List<StudentInfo> listStu = new List<StudentInfo>();
				CourseInfo courseInfo = course_BLL.GetCourses().Where(x => x.Id == courseid).FirstOrDefault()!;
				foreach (var studnet in student_BLL.GetStudents())
				{
					foreach (var course in studnet.CourseList)
					{
						if (course.Id == courseid)
						{
							listStu.Add(studnet);
						}
					}
				}
				//把集合，重新赋值给DGV
				dgvStudentInfo.DataSource = listStu;
			}
		}

		private void btnUpdateCourseInfo_Click(object sender, EventArgs e)
		{
			int id = Convert.ToInt32(dgvCourseInfo.SelectedRows[0].Cells["CourseId"].Value);
			frmUpdateCourse frmUpdateCourse = new frmUpdateCourse(id);
			frmUpdateCourse.ShowDialog();
		}

		private void btnDeleteCourseInfo_Click(object sender, EventArgs e)
		{
			int courseid = Convert.ToInt32(dgvCourseInfo.SelectedRows[0].Cells["CourseId"].Value);
			course_BLL.DeleteCourseInfoById(courseid);
			frmAdmin_Student_Load(this, e);
		}

		private void txtSearch_TextChanged(object sender, EventArgs e)
		{
			//只要文本框中的数据，发生了变化，就执行查询操作
			//到底是按照拼音查，还是按照编号查

			//判断用户的输入是啥？
			//用户有没有输入
			if (txtSearch.Text.Length != 0)
			{
				if (txtSearch.Text.All(char.IsDigit))   //0-9
				{
					//按学号搜索
					dgvStudentInfo.DataSource = student_BLL.GetStudentInfoBySpellOrNum(txtSearch.Text.Trim(), 1);
				}
				else
				{
					//按汉字搜索
					dgvStudentInfo.DataSource = student_BLL.GetStudentInfoBySpellOrNum(txtSearch.Text.Trim(), 2);
				}
			}
			else
			{
				//展示所有的商品
				LoadStudentInfo();
			}
		}

		private void btnUpdateStuInfo_Click(object sender, EventArgs e)
		{
			if(dgvStudentInfo.DataSource == null)
			{ 
				return;
			}
			int id = Convert.ToInt32(dgvStudentInfo.SelectedRows[0].Cells["Id"].Value);
			frmUpdateStudent frmUpdateStudent = new frmUpdateStudent(id);
			frmUpdateStudent.ShowDialog();
			LoadStudentInfo();
		}

		private void btnDeleteStuInfo_Click(object sender, EventArgs e)
		{
			if (dgvStudentInfo.DataSource == null)
			{
				return;
			}
			int studentid = Convert.ToInt32(dgvStudentInfo.SelectedRows[0].Cells["Id"].Value);
			student_BLL.DeleteStudentInfoById(studentid);
			LoadStudentInfo();
		}

		private void dgvStudentInfo_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{

		}
	}
}
